package www.xpu.edu.app;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Activity_reg extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        //取消标题栏
        ActionBar actionbar = getSupportActionBar();
        if (actionbar != null) {
            actionbar.hide();
        }
        final EditText edit1 = (EditText) findViewById(R.id.reg_edit1);
        final EditText edit2 = (EditText) findViewById(R.id.reg_edit2);
        final EditText edit3 = (EditText) findViewById(R.id.reg_edit3);
        final Button button = (Button) findViewById(R.id.reg_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String num, psw1, psw2;
                num = edit1.getText().toString().trim();
                psw1 = edit2.getText().toString().trim();
                psw2 = edit3.getText().toString().trim();
                if (num.length() == 11 && psw1.equals(psw2)) {
                    Toast.makeText(Activity_reg.this, "校验通过", Toast.LENGTH_SHORT).show();
                    sendReguest(num,psw1);
                } else {
                    Toast.makeText(Activity_reg.this, "校验失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
//        http://localhost:8080/xpu/reg1?num=19829088783&password=12345678
    }

    public void sendReguest(final String user, final String password) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
                //        http://localhost:8080/xpu/reg1?num=19829088783&password=12345678

                try {
                    //局域网
                    //URL url = new URL("http://192.168.123.196:8080/xpu/reg1" + "?num=" + user + "&password=" + password);
                    //http://188.131.250.4:8080
                    URL url = new URL("http://188.131.250.4:8080/xpu/reg1" + "?num=" + user + "&password=" + password);
                    connection = (HttpURLConnection) url.openConnection();
                    //connection.setRequestMethod("GET");
                    connection.setRequestMethod("POST");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    InputStream ips = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(ips));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    showResponse(response.toString());
                    /*if (!response.toString().trim().equals("0"))
                        startActivity(new Intent(Activity_reg.this, Activity_change.class));*/

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void showResponse(final String response) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!response.toString().trim().equals("0"))
                    Toast.makeText(Activity_reg.this, "注册成功,目前账号处于待审核状态", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(Activity_reg.this, "此账号已存在或系统出现错误", Toast.LENGTH_LONG).show();
            }
        });
    }
}
